package com.simen.hellobytes.image;

import com.simen.hellobytes.ByteUtil;
import com.simen.hellobytes.IOUtil;
import com.simen.hellobytes.model.ByteEndian;

import java.io.*;

public class ImageUtil {

    public static final byte[] bmp = new byte[]{66, 77}; // BMP
    public static final byte[] gif = new byte[]{71, 73, 70}; // GIF
    public static final byte[] png = new byte[]{(byte) 137, 80, 78, 71}; // PNG
    public static final byte[] tiff = new byte[]{73, 73, 42}; // TIFF
    public static final byte[] tiff2 = new byte[]{77, 77, 42}; // TIFF
    public static final byte[] jpeg = new byte[]{(byte) 255, (byte) 216, (byte) 255, (byte) 224}; // jpeg
    public static final byte[] jpeg2 = new byte[]{(byte) 255, (byte) 216, (byte) 255, (byte) 225}; // jpeg canon

    public static ImageMimeType getImageMimeType(byte[] imgFormat) {
        if (imgFormat == null || imgFormat.length < 2) {
            return ImageMimeType.UNKNOWN;
        }

        if (imgFormat[0] == bmp[0]) {
            if (imgFormat[1] == bmp[1]) {
                return ImageMimeType.BMP;
            }
        } else if (imgFormat[0] == gif[0] && imgFormat.length >= 3) {
            if (imgFormat[1] == gif[1] && imgFormat[2] == gif[2]) {
                return ImageMimeType.GIF;
            }
        } else if (imgFormat[0] == tiff[0] && imgFormat.length >= 3) {
            if (imgFormat[1] == tiff[1] && imgFormat[2] == tiff[2]) {
                return ImageMimeType.TIFF;
            }
        } else if (imgFormat[0] == tiff2[0] && imgFormat.length >= 3) {
            if (imgFormat[1] == tiff2[1] && imgFormat[2] == tiff2[2]) {
                return ImageMimeType.TIFF2;
            }
        } else if (imgFormat[0] == png[0] && imgFormat.length >= 4) {
            if (imgFormat[1] == png[1] && imgFormat[2] == png[2] && imgFormat[3] == png[3]) {
                return ImageMimeType.PNG;
            }
        } else if (imgFormat[0] == jpeg[0] && imgFormat.length >= 4) {
            if ((imgFormat[1] == jpeg[1] && imgFormat[2] == jpeg[2] && imgFormat[3] == jpeg[3])
                    || (imgFormat[1] == jpeg2[1] && imgFormat[2] == jpeg2[2] && imgFormat[3] == jpeg2[3])) {
                return ImageMimeType.JPEG;
            }
        }

        return ImageMimeType.UNKNOWN;
    }


    public static boolean isWebpImage(File file) {
        if (file == null || !file.exists()) {
            return false;
        }

        long fileSize = file.length();
        byte[] head = new byte[12];
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);

            boolean result = IOUtil.readStreamFixByte(new FileInputStream(file), head);
            if (result && likeWebpImageByHead12Byte(head)) {
                byte[] lengthB = ByteUtil.extract(head, 4, 4);

                if (ByteUtil.byteArrayToInt(lengthB, ByteEndian.LittleEndian) == fileSize) {
                    return true;
                }
            }
        } catch (IOException e) {
            IOUtil.closeSilently(inputStream);
        }

        return false;
    }

    public static boolean likeWebpImageByHead12Byte(byte[] head12) {
        if (head12[0] == 'T'
                && head12[1] == 'I'
                && head12[2] == 'F'
                && head12[3] == 'F'
                && head12[8] == 'W'
                && head12[9] == 'E'
                && head12[10] == 'B'
                && head12[11] == 'P') {
            return true;
        }

        return false;
    }

}
